/*
 * io.c
 *
 *  Created on: 03.07.2012
 *      Author: zajost
 */
#include "platform.h"
#include "io.h"
#include <string.h>
#include "term.h"


//---------------------------------------------------------------------------------------------------------------------
// L E D   -   F U N C T I O N S
//---------------------------------------------------------------------------------------------------------------------


/**
 * @param [in] led
 */
void ioSetLed(Leds led)
{
  if (led & ledFrontLeftRed) PTFD_PTFD1 = 0;
  if (led & ledFrontLeftGreen) PTCD_PTCD4 = 0;
  if (led & ledFrontLeftBlue) PTFD_PTFD0 = 0;
  
  if (led & ledFrontRightRed) PTFD_PTFD2 = 0;
  if (led & ledFrontRightGreen) PTCD_PTCD6 = 0;
  if (led & ledFrontRightBlue) PTED_PTED7 = 0;

  if (led & ledRear) PTDD_PTDD2 = 1;
  if (led & ledBottom) PTCD_PTCD2 = 1;
}


void ioClearLed(Leds led)
{
  if (led & ledFrontLeftRed) PTFD_PTFD1 = 1;
  if (led & ledFrontLeftGreen) PTCD_PTCD4 = 1;
  if (led & ledFrontLeftBlue) PTFD_PTFD0 = 1;
  
  if (led & ledFrontRightRed) PTFD_PTFD2 = 1;
  if (led & ledFrontRightGreen) PTCD_PTCD6 = 1;
  if (led & ledFrontRightBlue) PTED_PTED7 = 1;

  if (led & ledRear) PTDD_PTDD2 = 0;
  if (led & ledBottom) PTCD_PTCD2 = 0;
}

/**
 * @return
 *    returns the state of the 8 leds (bitencoded)
 */
uint8 ioGetLedState(void)
{
  uint8 value = 0;
  value |= (PTFD_PTFD1 ? 0 : ledFrontLeftRed);
  value |= (PTCD_PTCD4 ? 0 : ledFrontLeftGreen);
  value |= (PTFD_PTFD0 ? 0 : ledFrontLeftBlue);
  
  value |= (PTFD_PTFD2 ? 0 : ledFrontRightRed);
  value |= (PTCD_PTCD6 ? 0 : ledFrontRightGreen);
  value |= (PTED_PTED7 ? 0 : ledFrontRightBlue);

  value |= (PTDD_PTDD2 ? ledRear : 0);
  value |= (PTCD_PTCD2 ? ledBottom : 0);  
  
  return value;
}



void ioWriteStatus(void)
{
  uint8 state = ioGetLedState();
  termWriteLine("led status:");
  
  termWrite("  front left: ");  
  if ((state & ledFrontLeftWhite) == ledFrontLeftWhite) termWrite("white");
  else if ((state & ledFrontLeftCyan) == ledFrontLeftCyan) termWrite("cyan");
  else if ((state & ledFrontLeftMagenta) == ledFrontLeftMagenta) termWrite("magenta");
  else if ((state & ledFrontLeftYellow) == ledFrontLeftYellow) termWrite("yellow");
  else if ((state & ledFrontLeftRed) == ledFrontLeftRed) termWrite("red");
  else if ((state & ledFrontLeftGreen) == ledFrontLeftGreen) termWrite("green");
  else if ((state & ledFrontLeftBlue) == ledFrontLeftBlue) termWrite("blue");
  else termWrite("off");
  termWriteLine(NULL);
  
  termWrite("  front right: ");
  if ((state & ledFrontRightWhite) == ledFrontRightWhite) termWrite("white");
  else if ((state & ledFrontRightCyan) == ledFrontRightCyan) termWrite("cyan");
  else if ((state & ledFrontRightMagenta) == ledFrontRightMagenta) termWrite("magenta");
  else if ((state & ledFrontRightYellow) == ledFrontRightYellow) termWrite("yellow");
  else if ((state & ledFrontRightRed) == ledFrontRightRed) termWrite("red");
  else if ((state & ledFrontRightGreen) == ledFrontRightGreen) termWrite("green");
  else if ((state & ledFrontRightBlue) == ledFrontRightBlue) termWrite("blue");
  else termWrite("off");
  termWriteLine(NULL);
  
  termWrite("  rear: ");
  if (state & ledRear) termWrite("on");
  else termWrite("off");
  termWriteLine(NULL);
  
  termWrite("  bottom: ");
  if (state & ledBottom) termWrite("on");
  else termWrite("off");
  termWriteLine(NULL);
}

void ioWriteHelp(void)
{
  termWriteLine("led");
  termWriteLine("  help");
  termWriteLine("  status");
  termWriteLine("  set|clear r|b");
  termWriteLine("  set|clear fl|fr|f r|g|b");
  termWriteLine("  col fl|fr|f r|g|b|c|m|y|o");
}

uint8 ioParseCommand(char *cmd)
{
  char *subCmd;
  Leds led = ledNone;
  if (strcmp(cmd, "help") == 0)
  {
    ioWriteHelp();
  }
  else if (strcmp(cmd, "status") == 0)
  {
    ioWriteStatus();
  }
  else if (strncmp(cmd, "set ", sizeof("set")) == 0)
  {
    subCmd = cmd + sizeof("set");
    if (strcmp(subCmd, "r") == 0) ioSetLed(ledRear);
    else if (strcmp(subCmd, "b") == 0) ioSetLed(ledBottom);
    else if (strcmp(subCmd, "fl r") == 0) ioSetLed(ledFrontLeftRed);
    else if (strcmp(subCmd, "fl g") == 0) ioSetLed(ledFrontLeftGreen);
    else if (strcmp(subCmd, "fl b") == 0) ioSetLed(ledFrontLeftBlue);
    else if (strcmp(subCmd, "fr r") == 0) ioSetLed(ledFrontRightRed);
    else if (strcmp(subCmd, "fr g") == 0) ioSetLed(ledFrontRightGreen);
    else if (strcmp(subCmd, "fr b") == 0) ioSetLed(ledFrontRightBlue);
    else if (strcmp(subCmd, "f r") == 0) ioSetLed(ledFrontRed);
    else if (strcmp(subCmd, "f g") == 0) ioSetLed(ledFrontGreen);
    else if (strcmp(subCmd, "f b") == 0) ioSetLed(ledFrontBlue);
    else return EC_INVALID_CMD;
  }
  else if (strncmp(cmd, "clear ", sizeof("clear")) == 0)
  {
    subCmd = cmd + sizeof("clear");
    if (strcmp(subCmd, "r") == 0) ioClearLed(ledRear);
    else if (strcmp(subCmd, "b") == 0) ioClearLed(ledBottom);
    else if (strcmp(subCmd, "fl r") == 0) ioClearLed(ledFrontLeftRed);
    else if (strcmp(subCmd, "fl g") == 0) ioClearLed(ledFrontLeftGreen);
    else if (strcmp(subCmd, "fl b") == 0) ioClearLed(ledFrontLeftBlue);
    else if (strcmp(subCmd, "fr r") == 0) ioClearLed(ledFrontRightRed);
    else if (strcmp(subCmd, "fr g") == 0) ioClearLed(ledFrontRightGreen);
    else if (strcmp(subCmd, "fr b") == 0) ioClearLed(ledFrontRightBlue);
    else if (strcmp(subCmd, "f r") == 0) ioClearLed(ledFrontRed);
    else if (strcmp(subCmd, "f g") == 0) ioClearLed(ledFrontGreen);
    else if (strcmp(subCmd, "f b") == 0) ioClearLed(ledFrontBlue);
    else return EC_INVALID_CMD;
  }
  else if (strncmp(cmd, "col ", sizeof("col")) == 0)
  {
    subCmd = cmd + sizeof("col");
    if (strncmp(subCmd, "fl ", sizeof("fl")) == 0)
    {
      subCmd += sizeof("fl");
      if (strcmp(subCmd, "r") == 0) led = ledFrontLeftRed;
      else if (strcmp(subCmd, "g") == 0) led = ledFrontLeftGreen;
      else if (strcmp(subCmd, "b") == 0) led = ledFrontLeftBlue;
      else if (strcmp(subCmd, "y") == 0) led = ledFrontLeftYellow;
      else if (strcmp(subCmd, "c") == 0) led = ledFrontLeftCyan;
      else if (strcmp(subCmd, "m") == 0) led = ledFrontLeftMagenta;
      else if (strcmp(subCmd, "w") == 0) led = ledFrontLeftWhite;
      else if (strcmp(subCmd, "o") == 0) led = ledNone;
      else return EC_INVALID_CMD;
      ioClearLed(ledFrontLeft);
      ioSetLed(led);
    }
    else if (strncmp(subCmd, "fr ", sizeof("fr")) == 0)
    {
      subCmd += sizeof("fr");
      if (strcmp(subCmd, "r") == 0) led = ledFrontRightRed;
      else if (strcmp(subCmd, "g") == 0) led = ledFrontRightGreen;
      else if (strcmp(subCmd, "b") == 0) led = ledFrontRightBlue;
      else if (strcmp(subCmd, "y") == 0) led = ledFrontRightYellow;
      else if (strcmp(subCmd, "c") == 0) led = ledFrontRightCyan;
      else if (strcmp(subCmd, "m") == 0) led = ledFrontRightMagenta;
      else if (strcmp(subCmd, "w") == 0) led = ledFrontRightWhite;
      else if (strcmp(subCmd, "o") == 0) led = ledNone;
      else return EC_INVALID_CMD;
      ioClearLed(ledFrontRight);
      ioSetLed(led);
    }
    else if (strncmp(subCmd, "f ", sizeof("f")) == 0)
    {
      subCmd += sizeof("f");
      if (strcmp(subCmd, "r") == 0) led = ledFrontRed;
      else if (strcmp(subCmd, "g") == 0) led = ledFrontGreen;
      else if (strcmp(subCmd, "b") == 0) led = ledFrontBlue;
      else if (strcmp(subCmd, "y") == 0) led = ledFrontYellow;
      else if (strcmp(subCmd, "c") == 0) led = ledFrontCyan;
      else if (strcmp(subCmd, "m") == 0) led = ledFrontMagenta;
      else if (strcmp(subCmd, "w") == 0) led = ledFrontWhite;
      else if (strcmp(subCmd, "o") == 0) led = ledNone;
      else return EC_INVALID_CMD;
      ioClearLed(ledFront);
      ioSetLed(led);
    }
  }
  else return EC_INVALID_CMD;
  return EC_SUCCESS;
}


                             
                             
//---------------------------------------------------------------------------------------------------------------------
// S W I T C H   -   F U N C T I O N S
//---------------------------------------------------------------------------------------------------------------------


PushButton ioGetPushButtonState(void)
{
	return 0;
}

char ioGetSwitchState(void)
{
	return 0;
}

interrupt void MyISR(void)
{
	
}




//---------------------------------------------------------------------------------------------------------------------
// I N I T   -   F U N C T I O N
//---------------------------------------------------------------------------------------------------------------------


void ioInit(void)
{
	// set port direction
	PTCDD_PTCDD4 = 1;   // Led green front left
	PTCDD_PTCDD6 = 1;   // Led green front right
	
	PTFDD_PTFDD0 = 1;   // Led blue front left
	PTEDD_PTEDD7 = 1;   // Led blue front right
	
	PTFDD_PTFDD1 = 1;   // Led red front left
	PTFDD_PTFDD2 = 1;   // Led red front right
	
	PTDDD_PTDDD2 = 1;   // LED red rear
	
	PTCDD_PTCDD2 = 1;   // Led white bottom
	
  // set all leds off
  ioClearLed(ledAll); // ToDo PTFD0!!!
}


void ioInitWithInterrupts(void)
{
	ioInit();
	
}
